CC=gcc
FLAGS= -I"sources" -w -g
LFLAGS= -L $(BINARIES) -l"my_thread"
#Wall
SOURCES=sources
BINARIES=binaries
TEST=tests
THREAD_TOOLS=$(BINARIES)/my_thread.o $(BINARIES)/ring_thread_list.o
PREEMPTION_THREAD_TOOLS= $(BINARIES)/thread_preemption.o $(BINARIES)/ring_thread_list.o $(BINARIES)/preemption.o
OPT_THREAD_TOOLS=$(BINARIES)/opt_thread.o $(BINARIES)/ring_thread_list.o
#
# Program
# ----------------------------------------------------------------------------------------------
all: test test_opt
	$(CC) -shared $(THREAD_TOOLS) -o $(BINARIES)/my_thread.so
	$(CC) -o my_thread $(THREAD_TOOLS) $(BINARIES)/ex.o
opt_thread_exemple: opt_th exemple
	$(CC) -o test_opt_thread $(OPT_THREAD_TOOLS) $(BINARIES)/ex.o

preemption_thread: thread_preemption priority_exemple
	$(CC) -o thread_preemption $(PREEMPTION_THREAD_TOOLS) $(BINARIES)/pex.o
check: test all preemption thread
	./ring_list_test;./capacity_test 50000;
	@clear
	@echo Starting fibonacci with 20 ...
	@sleep 1 
	@time ./fibonacci 20
	@clear 
	@echo Starting the normal example ...
	@time ./my_thread
	@echo Starting the normal example with preemption and priority...
	@time ./thread_preemption
opt_check: test_opt
	./ring_list_test;./opt_capacity_test 50000;
	@clear
	@echo Starting optimal fibonacci with 27 ...
	@sleep 1 
	@time ./opt_fibonacci 27
	@clear 
	@echo Starting the optimal semaphore on example ...
	@time ./opt_semaphore_test
	@clear
	@echo Starting the optimal example ...
	@time ./test_opt_thread
	
#
# Tests
#-------------------------------------------------------------------------------------------------
test: test_my_thread preemption_thread test_capacity test_list test_fibo test_sem ptest_capacity ptest_fibo ptest_sem test_optsem
test_opt: test_opt_capacity test_opt_fibo test_optsem opt_thread_exemple
test_list: thread
	$(CC) $(FLAGS) -o $(BINARIES)/ring_list_test.o -c $(TEST)/ring_list_test.c
	$(CC) -o ring_list_test $(BINARIES)/ring_list_test.o $(THREAD_TOOLS)
test_my_thread: thread exemple
	$(CC) -o my_thread $(THREAD_TOOLS) $(BINARIES)/ex.o 
test_capacity: thread
	$(CC) $(FLAGS) -o $(BINARIES)/capacity_test.o -c $(TEST)/capacity_test.c
	$(CC) -o capacity_test $(BINARIES)/capacity_test.o $(THREAD_TOOLS)
test_opt_capacity: opt_th
	$(CC) $(FLAGS) -o $(BINARIES)/capacity_test.o -c $(TEST)/capacity_test.c
	$(CC) -o opt_capacity_test $(BINARIES)/capacity_test.o $(OPT_THREAD_TOOLS)  
test_fibo: thread
	$(CC) $(FLAGS) -o $(BINARIES)/fibonacci.o -c $(TEST)/fibonacci.c
	$(CC) -o fibonacci $(BINARIES)/fibonacci.o $(THREAD_TOOLS)
test_opt_fibo: opt_thread
	$(CC) $(FLAGS) -o $(BINARIES)/fibonacci.o -c $(TEST)/fibonacci.c
	$(CC) -o opt_fibonacci $(BINARIES)/fibonacci.o $(OPT_THREAD_TOOLS)

test_sem: thread semaphore
	$(CC) $(FLAGS) -o $(BINARIES)/semaphore_test.o -c $(TEST)/semaphore_test.c
	$(CC) -o semaphore_test $(BINARIES)/semaphore_test.o $(BINARIES)/semaphore.o  $(THREAD_TOOLS)

test_optsem: opt_th optsemaphore
	$(CC) $(FLAGS) -o $(BINARIES)/optsemaphore_test.o -c $(TEST)/optsemaphore_test.c
	$(CC) -o opt_semaphore_test $(BINARIES)/optsemaphore_test.o $(BINARIES)/optsemaphore.o  $(THREAD_TOOLS)
ptest_capacity: thread thread_preemption
	$(CC) $(FLAGS) -o $(BINARIES)/capacity_test.o -c $(TEST)/capacity_test.c
	$(CC) -o pcapacity_test $(BINARIES)/capacity_test.o $(PREEMPTION_THREAD_TOOLS) 
ptest_fibo: thread thread_preemption
	$(CC) $(FLAGS) -o $(BINARIES)/fibonacci.o -c $(TEST)/fibonacci.c
	$(CC) -o pfibonacci $(BINARIES)/fibonacci.o $(PREEMPTION_THREAD_TOOLS)
ptest_sem: thread semaphore thread_preemption
	$(CC) $(FLAGS) -o $(BINARIES)/semaphore_test.o -c $(TEST)/semaphore_test.c
	$(CC) -o psemaphore_test $(BINARIES)/semaphore_test.o $(BINARIES)/semaphore.o  $(PREEMPTION_THREAD_TOOLS)
clean:
	rm -f $(BINARIES)/*
mrpoper: clean
	rm -f capacity_test
	rm -f ring_list_test
	rm -f fibonacci
	rm -f semaphore_test
	rm -f my_thread
	rm -f opt_thread


#
# Declarations
# ----------------------------------------------------------------------------------------------
thread: $(SOURCES)/thread.c $(SOURCES)/ring_thread_list.c
	$(CC) -o $(BINARIES)/my_thread.o -c $(SOURCES)/thread.c $(FLAGS)
	$(CC) -o $(BINARIES)/ring_thread_list.o -c $(SOURCES)/ring_thread_list.c $(FLAGS)
opt_th:
	$(CC) -o $(BINARIES)/opt_thread.o -c $(SOURCES)/optthread/opt_thread.c $(FLAGS)
	$(CC) -o $(BINARIES)/ring_thread_list.o -c $(SOURCES)/ring_thread_list.c $(FLAGS)
	
thread_preemption: preemption $(SOURCES)/preemption/thread_preemption.c $(SOURCES)/ring_thread_list.c
	$(CC) -o $(BINARIES)/thread_preemption.o -c $(SOURCES)/preemption/thread_preemption.c $(FLAGS)
	$(CC) -o $(BINARIES)/ring_thread_list.o -c $(SOURCES)/ring_thread_list.c $(FLAGS)
semaphore: $(SOURCES)/semaphore/semaphore.c $(SOURCES)/semaphore/semaphore.h
	 $(CC) $(FLAGS) -o $(BINARIES)/semaphore.o -c $(SOURCES)/semaphore/semaphore.c
optsemaphore: $(SOURCES)/semaphore/optsemaphore.c $(SOURCES)/semaphore/optsemaphore.h
	 $(CC) $(FLAGS) -o $(BINARIES)/optsemaphore.o -c $(SOURCES)/semaphore/optsemaphore.c
preemption: $(SOURCES)/preemption/preemption.c $(SOURCES)/preemption/preemption.h
	$(CC) $(FLAGS) -o $(BINARIES)/preemption.o -c $(SOURCES)/preemption/preemption.c
exemple: $(TEST)/example.c
	$(CC) -o $(BINARIES)/ex.o -c $(TEST)/example.c $(FLAGS)
priority_exemple: $(TEST)/priority_example.c
	$(CC) -o $(BINARIES)/pex.o -c $(TEST)/priority_example.c $(FLAGS)
